//
//  QpLocalTravelViewController.m
//  Tour
//
//  Created by 才诗琦 on 16/3/11.
//  Copyright © 2016年 张中烨. All rights reserved.
//

#import "QpLocalTravelViewController.h"
#import "PrefixHeader.pch"

@interface QpLocalTravelViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
{
    NSInteger _page;
}
@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *travelArr;        /**< 自由行数组 */
@property (nonatomic, retain) NSMutableArray *localArr;         /**< 当地游数组 */
@property (nonatomic, retain) UICollectionView *collectionView;
@property (nonatomic, retain) UIButton *travel;                 /**< 超值自由行 */
@property (nonatomic, retain) UIButton *local;                  /**< 精彩当地游 */
@property (nonatomic, retain) UILabel *xiantiao;                /**< btn 下滚动条 */
@end

@implementation QpLocalTravelViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"特价折扣";
   
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"返回"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    

    // tableView数组初始化
    self.travelArr = [NSMutableArray array];
    _page = 1;
    /**< 初值 */
    [self dataHandlertravel];
    [self dataHandlerlocal];
    
    self.local = [UIButton buttonWithType:UIButtonTypeSystem];
    _local.frame =CGRectMake(SCREEN_WIDTH / 2 , 0, SCREEN_WIDTH / 2 , 30);
    _local.backgroundColor = [UIColor whiteColor];
    [_local setTitle:@"精彩当地游" forState:UIControlStateNormal];
    [_local setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [_local addTarget:self action:@selector(local:) forControlEvents:UIControlEventTouchUpInside];
//    [_local NightWithType:UIViewColorTypeNormal];
    [self.view addSubview:_local];
    
    self.travel = [UIButton buttonWithType:UIButtonTypeSystem];
    _travel.frame =  CGRectMake(0, 0, SCREEN_WIDTH / 2  , 30);
    _travel.backgroundColor = [UIColor whiteColor];
    [_travel setTitle:@"超值自由行" forState:UIControlStateNormal];
    [_travel setTitleColor:[UIColor colorWithRed:64/255.0 green:168/255.0 blue:119/255.0 alpha:1] forState:UIControlStateNormal];
    [_travel addTarget:self action:@selector(travel:) forControlEvents:UIControlEventTouchUpInside];
//    [_travel NightWithType:UIViewColorTypeNormal];
    [self.view addSubview:_travel];
    
    // 线
    UILabel *xian = [[UILabel alloc]initWithFrame:CGRectMake(0, 93 - 64, SCREEN_WIDTH, 1)];
    xian.backgroundColor = [UIColor lightGrayColor];
    
    [self.view addSubview:xian];

    //
    self.xiantiao = [[UILabel alloc]initWithFrame:CGRectMake(50, 93 - 64, 150, 1)];
    _xiantiao.backgroundColor = [UIColor colorWithRed:64/255.0 green:168/255.0 blue:119/255.0 alpha:1];
    _xiantiao.center = CGPointMake(SCREEN_WIDTH / 4, 93 - 64);
    [self.view addSubview:_xiantiao];
;
    // 创建collectionView
    [self creatCollectionView];

}

- (void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark -  创建collectionView
- (void)creatCollectionView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    // 设置collectionview滚动方向为水平
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    // 设置cell大小
    layout.itemSize = CGSizeMake(SCREEN_WIDTH, self.view.frame.size.height - 64);
    layout.minimumInteritemSpacing = 20;
    //    layout.minimumLineSpacing = 10;
    // 边界
    //    layout.sectionInset = UIEdgeInsetsMake(0, 10, 0, 10);
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 30, SCREEN_WIDTH, SCREEN_HEIGHT - 30) collectionViewLayout:layout];
    // 设置大小
    _collectionView.contentSize = CGSizeMake(SCREEN_WIDTH * 2 ,SCREEN_HEIGHT );
    // 与tableView 相似
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    // 弹动效果
    _collectionView.bounces = NO;
    // 背景颜色
    _collectionView.backgroundColor = [UIColor whiteColor];
    // 隐藏滚动条
    _collectionView.showsHorizontalScrollIndicator = NO;
    _collectionView.showsVerticalScrollIndicator = NO;
    // 整页滑动
    _collectionView.pagingEnabled = YES;
    _collectionView.contentInset = UIEdgeInsetsMake(0, 0, 0, 20);
    // 注册重用池
    [_collectionView registerClass:[QpSiteTravelCollectionViewCell class] forCellWithReuseIdentifier:@"travelcell"];
    [_collectionView registerClass:[QpSiteLocalCollectionViewCell class] forCellWithReuseIdentifier:@"localcell"];
//    [_collectionView NightWithType:UIViewColorTypeNormal];
    [self.view addSubview:_collectionView];

    
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (self.travelArr.count != 0) {
        return 2;
    }
    return 0;
}

// 设置item 内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.item == 0) {
        QpSiteTravelCollectionViewCell *travelcell = [collectionView dequeueReusableCellWithReuseIdentifier:@"travelcell" forIndexPath:indexPath];
        
        travelcell.strid = self.idstr;
        BLOCK push = ^(NSString *pushid)
        {
            
            WebViewController *wv = [[WebViewController alloc]init];
            wv.urlstr = pushid;
                        NSLog(@"111111%@", pushid);
            [self.navigationController pushViewController:wv animated:YES];
            
            
        };
        travelcell.pushwv = push;
        
        
        return travelcell;
    }else {
        QpSiteLocalCollectionViewCell *localcell = [collectionView dequeueReusableCellWithReuseIdentifier:@"localcell" forIndexPath:indexPath];
        localcell.strid = self.idstr;
        BLOCK tiaozhuan = ^(NSString *pushurl)
        {
            WebViewController *wv = [[WebViewController alloc]init];
            wv.urlstr = pushurl;
            [self.navigationController pushViewController:wv animated:YES];

        };
        localcell.pushweb = tiaozhuan;
        return localcell;
    }
}

#pragma mark - travel 数据处理
- (void)dataHandlertravel
{
    NSString *string = [NSString stringWithFormat:@"http://open.qyer.com/qyer/discount/tickets_freewalker?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&id=%@&oauth_token=89dd34fbffaaa72503b4baa7a9d02812&order=2&page=%ld", self.idstr,(long)_page];
    NSString *url = @"&product_type=1016%2C1018%2C1020&time=1&track_app_channel=App%2520Store&track_app_version=6.8.3&track_device_info=iPhone%25204%28ChinaMobile%2CChinaUnicom%29&track_deviceid=CDA3EEEB-61BF-473E-AFFF-32B6332C9BBF&track_os=ios%25207.1.2&track_user_id=7328550&type=2&v=1";
    [AFNModel GETWithHandler:[NSString stringWithFormat:@"%@%@",string,url] completion:^(id result) {
        for (NSDictionary *dic in result[@"data"][@"list"]) {
                        QpCityDetailModel *q = [QpCityDetailModel modelWithDic:dic];
                        [self.travelArr addObject:q];
                                    NSLog(@"%@",_travelArr);
                    }
                    [_collectionView reloadData];
    }];

}

#pragma mark - local 数据处理
- (void)dataHandlerlocal
{
    NSString *string = [NSString stringWithFormat:@"http://open.qyer.com/qyer/discount/local_discount?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&id=%@&oauth_token=89dd34fbffaaa72503b4baa7a9d02812&order=2&page=%ld",self.idstr, _page];
    NSString *url = @"&product_type=2410&time=1&track_app_channel=App%2520Store&track_app_version=6.8.3&track_device_info=iPhone%25204%28ChinaMobile%2CChinaUnicom%29&track_deviceid=CDA3EEEB-61BF-473E-AFFF-32B6332C9BBF&track_os=ios%25207.1.2&track_user_id=7328550&type=2&v=1";
    
    [AFNModel GETWithHandler:[NSString stringWithFormat:@"%@%@",string,url] completion:^(id result) {
        self.localArr =[QpCityDetailModel modelHanderWithArray:result[@"data"][@"list"]];
                [_collectionView reloadData];

    }];
   }

#pragma mark -  精彩与自由 btn切换
- (void)travel:(UIButton *)btn
{
    [_local setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [_travel setTitleColor:[UIColor colorWithRed:64/255.0 green:168/255.0 blue:119/255.0 alpha:1] forState:UIControlStateNormal];
    _xiantiao.center = CGPointMake(SCREEN_WIDTH / 4, 93 - 64);
    [_collectionView setContentOffset:CGPointMake(0, 0) animated:YES];
}
- (void)local:(UIButton *)btn
{
    
    [_travel setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [_local setTitleColor:[UIColor colorWithRed:64/255.0 green:168/255.0 blue:119/255.0 alpha:1] forState:UIControlStateNormal];
    _xiantiao.center = CGPointMake(SCREEN_WIDTH / 4 * 3, 93 -64);
    [_collectionView setContentOffset:CGPointMake(SCREEN_WIDTH, 0) animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
